$(".kjtz_btn").click(function() {
    if ($(".kjtz_btn").html() == '快捷投注') {
        $("#stat_play_ctn").hide();
        $("#kjtz-ctn").show();
        $(".kjtz_btn").html("长龙统计");
    }
    else {
        $("#stat_play_ctn").show();
        $("#kjtz-ctn").hide();
        $(".kjtz_btn").html("快捷投注");
    }
});

// 填充下数据
function fillKjtz() {
    var table = $("#kjtz-ctn tbody");
    table.empty();
    var arr = ["单码", "小单", "合单", "双码", "小双", "合双",
        "大码", "大单", "合大", "小码", "大双", "合小",
        "0头",  "0尾",  "5尾",  "1头", "1尾",  "6尾",
        "2头",  "2尾",  "7尾",  "3头", "3尾", "8尾",
        "4头",  "4尾",  "9尾",  "鼠",  "龙",   "猴",
        "牛",   "蛇",   "鸡",   "虎",  "马",   "狗",
        "兔",   "羊",   "猪",   "红",  "蓝",   "绿",
        "红单", "蓝单", "绿单", "红双", "蓝双", "绿双",
        "红大", "蓝大", "绿大", "红小", "蓝小", "绿小",
        "全选", "反选", "取消" ];
    var tr = $("<tr></tr>");
    for (var i = 0; i < arr.length; i++) {
        tr.append('<td><span>' + arr[i] + '</td>');
        if (i % 3 == 2) {
            table.append(tr);
            tr = $("<tr></tr>");
        }
    }

    $("#kjtz-ctn td").click(function(e) {
        var td = $(e.currentTarget);
        var arr = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
        var k = td.find("span").html();
        if (arr.indexOf(k) != -1) {
            // 12生肖需要特殊处理
            if (td.attr("class") && td.attr("class").indexOf("bg_yellow")) 
                td.removeClass("bg_yellow");
            else 
                td.addClass("bg_yellow");

            // 非生肖的都不要选中
            var tds = $("#kjtz-ctn td");
            for (var i = 0; i < tds.length; i++) {
                if (arr.indexOf($(tds[i]).find("span").html()) == -1) $(tds[i]).removeClass("bg_yellow");
            }

            _doKjtz(k);
            return;
        }

        $("#kjtz-ctn td").removeClass("bg_yellow");
        _doKjtz(k);
        td.addClass("bg_yellow");
    });
}
fillKjtz();

function _doKjtz(k) {
    var tid = $(".cate_menu a.selected").attr("tid");
    if (tid == 1) {
        var type = $("#tm_tabs .on").attr("TYPE");
        var key = "特码" + type;
        if (type == 'A') key = '特码';
        var c = $("#PAGE_" + type);
        var f = function(n) {
            var i = parseInt(n) < 10 ? '0' + parseInt(n) : parseInt(n);
            return key + i;
        };
    }
    else if (tid == 7) {
        var key = "正";
        var c = $(".zhengma");
        var f = function(n) {
            return key + n;
        }
    }
    else if (tid == 8) {
        var arr = ['一', '二', '三', '四', '五', '六'];
        var type = arr[$("#ztm_tabs .on").attr("value") - 1];
        var key = type + '码';
        var c = $("#ZMT_PAGE" + $("#ztm_tabs .on").attr("value"));
        var f = function(n) {
            var i = parseInt(n) < 10 ? '0' + parseInt(n) : parseInt(n);
            return key + i;
        };
    }
    else return;

    if (k[1] == '尾') {
        reset_counter();
        _wei(c, f, k[0]);
        return;
    }

    switch (k) {
        case "取消": reset_counter(); break;
        case "单码": {
            reset_counter();
            _danma(c, f);
            break;
        }
        case "双码": {
            reset_counter();
            _shuangma(c, f);
            break;
        }
        case "大码": {
            reset_counter();
            _dama(c, f);
            break;
        }
        case "小码": {
            reset_counter();
            _xiaoma(c, f);
            break;
        }
        case "0头": {
            reset_counter();
            _0tou(c, f);
            break;
        }
        case "1头": {
            reset_counter();
            _1tou(c, f);
            break;
        }
        case "2头": {
            reset_counter();
            _2tou(c, f);
            break;
        }
        case "3头": {
            reset_counter();
            _3tou(c, f);
            break;
        }
        case "4头": {
            reset_counter();
            _4tou(c, f);
            break;
        }
        case "小单": {
            reset_counter();
            _xiaodan(c, f);
            break;
        }
        case "小双": {
            reset_counter();
            _xiaoshuang(c, f);
            break;
        }
        case "大单": {
            reset_counter();
            _dadan(c, f);
            break;
        }
        case "大双": {
            reset_counter();
            _dashuang(c, f);
            break;
        }
        case "合单": {
            reset_counter();
            _hedan(c, f);
            break;
        }
        case "合双": {
            reset_counter();
            _heshuang(c, f);
            break;
        }
        case "合大": {
            reset_counter();
            _heda(c, f); 
            break;
        }
        case "合小": {
            reset_counter();
            _hexiao(c, f);
            break;
        }
        case "红": {
            reset_counter();
            _hong(c, f);
            break;
        }  
        case "蓝": {
            reset_counter();
            _lan(c, f);
            break;
        }  
        case "绿": {
            reset_counter();
            _lv(c, f);
            break;
        }   
        case "红单": {
            reset_counter();
            _hongdan(c, f);
            break;
        }  
        case "蓝单": {
            reset_counter();
            _landan(c, f);
            break;
        }  
        case "绿单": {
            reset_counter();
            _lvdan(c, f);
            break;
        }  
        case "红双": {
            reset_counter();
            _hongshuang(c, f);
            break;
        }  
        case "蓝双": {
            reset_counter();
            _lanshuang(c, f);
            break;
        }  
        case "绿双": {
            reset_counter();
            _lvshuang(c, f);
            break;
        }  
        case "红大": {
            reset_counter();
            _hongda(c, f);
            break;
        }  
        case "蓝大": {
            reset_counter();
            _landa(c, f);
            break;
        }  
        case "绿大": {
            reset_counter();
            _lvda(c, f);
            break;
        }  
        case "红小": {
            reset_counter();
            _hongxiao(c, f);
            break;
        }  
        case "蓝小": {
            reset_counter();
            _lanxiao(c, f);
            break;
        }  
        case "绿小": {
            reset_counter();
            _lvxiao(c, f);
            break;
        }  
        case "全选": {
            reset_counter();
            _quan(c, f);
            break;
        }  
        case "反选": {
            _fanxuan(c, f);
            break;
        }  

        // 12生肖
        default: {
            _shengxiao(c, f, k);
            break;
        }
    }
}

function _danma(c, f) {
    for (var i = 1; i <= 49; i += 2) {
        counter_select(f(i), c);
    }
}
function _shuangma(c, f) {
    for (var i = 2; i <= 49; i += 2) {
        counter_select(f(i), c);
    }
}
function _dama(c, f) {
    for (var i = 25; i <= 49; i++) {
        counter_select(f(i), c);
    }
}
function _xiaoma(c, f) {
    for (var i = 1; i <= 24; i++) {
        counter_select(f(i), c);
    }
}
function _0tou(c, f) {
    for (var i = 1; i <= 9; i++) {
        counter_select(f(i), c);
    }
}
function _1tou(c, f) {
    for (var i = 10; i <= 19; i++) {
        counter_select(f(i), c);
    }
}
function _2tou(c, f) {
    for (var i = 20; i <= 29; i++) {
        counter_select(f(i), c);
    }
}
function _3tou(c, f) {
    for (var i = 30; i <= 39; i++) {
        counter_select(f(i), c);
    }
}
function _4tou(c, f) {
    for (var i = 40; i <= 49; i++) {
        counter_select(f(i), c);
    }
}
function _xiaodan(c ,f) {
    for (var i = 1; i <= 23; i += 2) {
        counter_select(f(i), c);
    }
}
function _xiaoshuang(c, f) {
    for (var i = 2; i <= 22; i += 2) {
        counter_select(f(i), c);
    }
}
function _dadan(c, f) {
    for (var i = 25; i <= 49; i += 2) {
        counter_select(f(i), c);
    }
}
function _dashuang(c, f) {
    for (var i = 26; i <= 49; i += 2) {
        counter_select(f(i), c);
    }
}
function _hedan(c, f) {
    for (var i = 1; i <= 49; i += 2) {
        var v = parseInt(i/10) + (i%10);
        if (v % 2 == 1) {
            counter_select(f(i), c);
        }
    }
}
function _heshuang(c, f) {
    for (var i = 2; i <= 49; i += 2) {
        var v = parseInt(i/10) + (i%10);
        if (v % 2 == 0) {
            counter_select(f(i), c);
        }
    }
}
function _heda(c, f) {
    for (var i = 1; i <= 49; i++) {
        var v = parseInt(i/10) + (i%10);
        if (v > 6) {
            counter_select(f(i), c);
        }
    }
}
function _hexiao(c, f) {
    for (var i = 1; i <= 49; i++) {
        var v = parseInt(i/10) + (i%10);
        if (v <= 6) {
            counter_select(f(i), c);
        }
    }
}
function _wei(c, f, wei) {
    for (var i = 1; i <= 49; i++) {
        if (i % 10 == wei) {
            counter_select(f(i), c);
        }
    }
}
function _hong(c, f) {
    var list = gameRule.hongbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        counter_select(f(i), c);
    }
}
function _lan(c, f) {
    var list = gameRule.lanbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        counter_select(f(i), c);
    }
}
function _lv(c, f) {
    var list = gameRule.lvbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        counter_select(f(i), c);
    }
}
function _hongdan(c, f) {
    var list = gameRule.hongbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i % 2 == 0) continue;
        counter_select(f(i), c);
    }
}
function _landan(c, f) {
    var list = gameRule.lanbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i % 2 == 0) continue;
        counter_select(f(i), c);
    }
}
function _lvdan(c, f) {
    var list = gameRule.lvbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i % 2 == 0) continue;
        counter_select(f(i), c);
    }
}
function _hongshuang(c, f) {
    var list = gameRule.hongbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i % 2 == 1) continue;
        counter_select(f(i), c);
    }
}
function _lanshuang(c, f) {
    var list = gameRule.lanbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i % 2 == 1) continue;
        counter_select(f(i), c);
    }
}
function _lvshuang(c, f) {
    var list = gameRule.lvbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i % 2 == 1) continue;
        counter_select(f(i), c);
    }
}
function _hongda(c, f) {
    var list = gameRule.hongbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i < 25) continue;
        counter_select(f(i), c);
    }
}
function _landa(c, f) {
    var list = gameRule.lanbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i < 25) continue;
        counter_select(f(i), c);
    }
}
function _lvda(c, f) {
    var list = gameRule.lvbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i < 25) continue;
        counter_select(f(i), c);
    }
}

function _hongxiao(c, f) {
    var list = gameRule.hongbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i >= 25) continue;
        counter_select(f(i), c);
    }
}
function _lanxiao(c, f) {
    var list = gameRule.lanbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i >= 25) continue;
        counter_select(f(i), c);
    }
}
function _lvxiao(c, f) {
    var list = gameRule.lvbo();
    for (var kk in list) {
        var i = parseInt(list[kk]);
        if (i >= 25) continue;
        counter_select(f(i), c);
    }
}
function _quan(c, f) {
    for (var i = 1; i <= 49; i++) {
        counter_select(f(i), c);
    }
}
function _fanxuan(c, f) {
    for (var i = 1; i <= 49; i++) {
        var td = $('td.odds[value="__k__"]'.replace(/__k__/, f(i)));
        if (td.attr("class") && td.attr("class").indexOf("bg_yellow") == -1) counter_select(f(i), c);
        else counter_cancel(f(i), c);
    }
}

function _shengxiao(c, f, xiao) {
    var arr = $("#kjtz-ctn .bg_yellow span");
    var numbers = [];
    for (var i = 0; i < arr.length; i++) {
        var xiao = $(arr[i]).html();
        var list = gameRule.getXiaoNumbers(xiao);
        for (var j in list) {
            numbers.push(parseInt(list[j]));
        }
    }

    for (var i = 1; i <= 49; i++) {
        var j = i < 10 ? '0' + i : i;
        if (numbers.indexOf(i) == -1) {
            counter_cancel(f(i), c);
        }
        else
            counter_select(f(i), c);
    }
}